/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package jwu2indexserver;

import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.logging.Level;
import java.util.logging.Logger;
import jwu2util.net.Packet;

/**
 *
 * @author Rolf
 */
public class IndexServerConnectionThread implements Runnable {

    private Socket socket;
    private ObjectOutputStream out;
    private ObjectInputStream in;
    private boolean initOk;
    
    public IndexServerConnectionThread(Socket s) {
        socket = s;
        
        try {
            out = new ObjectOutputStream( socket.getOutputStream() );
            in = new ObjectInputStream( socket.getInputStream() );
            
            initOk = true;
        }
        catch(Exception e) {
            Logger.getLogger(IndexServerConnectionThread.class.getName()).log(Level.SEVERE, "Error getting input/output from socket", e);
            initOk = false;
        }
    }

    public void run() {
        if( !initOk ) {
            return;
        }
        
        Logger.getLogger(IndexServerConnectionThread.class.getName()).log(Level.INFO, "Starting client thread from ["+socket.getInetAddress()+"]...");
        
        boolean keepGoing = true;
        
        try {
            while(keepGoing) {
                Packet p = (Packet) in.readObject();

                Logger.getLogger(IndexServerConnectionThread.class.getName()).log(Level.INFO, "Received packet ["+p.getCommand()+"]");
                
                switch(p.getCommand()) {
                    
                    case VERSION:
                        sendVersionReply();
                        break;
                    case BYE:
                        keepGoing = false;
                        break;
                    default:
                        Logger.getLogger(IndexServerConnectionThread.class.getName()).log(Level.INFO, "Unknown protocol command: "+p.getCommand());
                }
            }
        }
        catch(Exception e) {
            Logger.getLogger(IndexServerConnectionThread.class.getName()).log(Level.INFO, "Connection to client ["+socket.getInetAddress()+"] closed");
        }

        Logger.getLogger(IndexServerConnectionThread.class.getName()).log(Level.INFO, "["+socket.getInetAddress()+"] disconnected...");
    }

    private void sendPacket(Packet p) {
        try {
            out.writeObject(p);
            out.flush();
            
            Logger.getLogger(IndexServerConnectionThread.class.getName()).log(Level.INFO, "Sent reply ["+p.getCommand()+"]");
        }
        catch(Exception e) {
            Logger.getLogger(IndexServerConnectionThread.class.getName()).log(Level.SEVERE, "Error sending reply", e);
        }
    }

    private void sendVersionReply() {
        Packet reply = new Packet(Packet.NetProtocol.VERSION);
        
        reply.addData(IndexServerCore.getSERVER_VERSION());
        
        sendPacket(reply);
    }
}
